package com.zat.mes.andrmes.customerview

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.LinearLayout
import com.zat.mes.andrmes.R
import kotlinx.android.synthetic.main.dialog_loading.view.*

/**
 * @author zy
 * @date :2020-02-13 22:40
 * @description: 通用加载loading
 */
class LoadingDialog {


    companion object {
        fun createLoading(context: Context): Dialog {
            val inflater = LayoutInflater.from(context);
            val v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
            val layout = v.dialog_loading_view// 加载布局

            val loadingDialog = Dialog(context, R.style.LoadingDialogStyle);// 创建自定义样式dialog
            loadingDialog.setCancelable(true); // 是否可以按“返回键”消失
            loadingDialog.setCanceledOnTouchOutside(false); // 点击加载框以外的区域
            loadingDialog.setContentView(
                layout, LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT
                )
            );// 设置布局
            /**
             *将显示Dialog的方法封装在这里面
             */
            val window = loadingDialog.getWindow();
            val lp = window?.getAttributes();
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp?.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window?.setGravity(Gravity.CENTER);
            window?.setAttributes(lp);
            loadingDialog.show();
            return loadingDialog;
        }

        fun closeLoading(loadingDialog: Dialog) {
            if (loadingDialog != null && loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }

        }
    }


}